﻿@using kangsuce.Model

@{
    ViewBag.Title = "MenuSet";
}

@section Css{
    <style type="text/css">
        .table th { text-align: center; }

        .firstMenu { font-weight: bold }

        .SecondMenu { padding-left: 30px !important }
    </style>
}
<div class="wrapper wrapper-content animated fadeInUp">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox">
                <div class="ibox-title">
                    <h5>微信菜单列表</h5>
                    <div class="ibox-tools">
                        <a onclick="add()" class="btn btn-primary btn-xs">添加主菜单</a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="project-list">
                        @*<table id="table"></table>*@

                        <table class="table table-bordered">
                            <thead>
                            <tr>
                                <th>菜单名称</th>
                                <th>跳转地址</th>
                                <th style="width: 100px">排序</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach (KSC_WxMenu item in ViewBag.List)
                            {
                                <tr>
                                    <td class="firstMenu">@item.menuName</td>
                                    @{
                                        string str = "";
                                        if (!string.IsNullOrWhiteSpace(item.menuUrl))
                                        {
                                            str = item.menuUrl;
                                        }
                                    }
                                    <td>@str</td>
                                    <td style="text-align: center">
                                        <i onclick="sortOrder('up', @item.menuId)" class="fa fa-level-up" style="color: red; cursor: pointer;"></i>&nbsp;&nbsp;
                                        <i onclick="sortOrder('down', @item.menuId)" class="fa fa-level-down" style="color: green; cursor: pointer;"></i>
                                    </td>
                                    <td>
                                        <button type="button" onclick="edit(@item.menuId)" class="btn btn-outline btn-primary btn-xs">编辑</button>
                                        <button type="button" onclick="del(@item.menuId)" class="btn btn-outline btn-primary btn-xs">删除</button>
                                        <button type="button" onclick="add(@item.menuId)" class="btn btn-outline btn-primary btn-xs">添加子菜单</button>
                                    </td>
                                </tr>
                                foreach (KSC_WxMenu chirdItem in item.chirdList)
                                {
                                    <tr>
                                        <td class="SecondMenu chird-@chirdItem.pId">@chirdItem.menuName</td>
                                        @{
                                            string chirdStr = "";
                                            if (!string.IsNullOrWhiteSpace(chirdItem.menuUrl))
                                            {
                                                chirdStr = chirdItem.menuUrl;
                                            }
                                        }
                                        <td>@chirdStr</td>
                                        <td style="text-align: center">
                                            <i onclick="sortOrder('up', @chirdItem.menuId)" class="fa fa-level-up" style="color: red; cursor: pointer;"></i>&nbsp;&nbsp;
                                            <i onclick="sortOrder('down', @chirdItem.menuId)" class="fa fa-level-down" style="color: green; cursor: pointer;"></i>
                                        </td>
                                        <td>
                                            <button type="button" onclick="edit(@chirdItem.menuId)" class="btn btn-outline btn-primary btn-xs">编辑</button>
                                            <button type="button" onclick="del(@chirdItem.menuId)" class="btn btn-outline btn-primary btn-xs">删除</button>
                                        </td>
                                    </tr>
                                }
                            }
                            </tbody>
                        </table>
                    </div>
                    <button class="btn btn-primary" onclick="saveToWx()" type="button">保存到微信</button>
                </div>
            </div>
        </div>
    </div>
</div>


@section Js{
    <script type="text/javascript">

        $(function() {

        });

        //添加
        function add(pid) {
            if (pid) {
                if ($(".chird-" + pid).length > 4) {
                    layer.msg("子菜单不能超过5个", { icon: 5, time: 2000 });
                    return;
                }
            } else {
                if ($(".firstMenu").length > 2) {
                    layer.msg("一级菜单不能超过3个", { icon: 5, time: 2000 });
                    return;
                }
            }
            layer.open({
                type: 2,
                area: ['700px', '440px'],
                title: '菜单-新增',
                fixed: false, //不固定
                content: ['/WxSet/WxMenuEdit?pid=' + pid, 'no']
            });
        }

//编辑
        function edit(id) {
            layer.open({
                type: 2,
                area: ['700px', '440px'],
                title: '菜单-编辑',
                fixed: false, //不固定
                content: ['/WxSet/WxMenuEdit?id=' + id, 'no']
            });
        }

//排序
        function sortOrder(sort, id) {
            $.getJSON('/WxSet/WxMenuOrderChange?sortOrder=' + sort + '&id=' + id, function(json) {
                if (json.result == "success") {
                    location.reload();
                } else {
                    layer.msg(json.msg, { icon: 5, time: 2000 });
                }
            });
        }

//删除
        function del(id) {
            layer.confirm('确认删除当前菜单', { icon: 3, title: '提示' }, function(index) {
                $.getJSON("/WxSet/WxMenuDelete?id=" + id, function(json) {
                    if (json.result == "success") {
                        layer.msg(json.msg, { icon: 1, time: 2000 }, function() {
                            location.reload();
                        });
                    } else {
                        layer.msg(json.msg, { icon: 5, time: 2000 });
                    }
                });
            });
        }

//保存到微信
        function saveToWx() {
            layer.confirm('确认保存到微信', { icon: 3, title: '提示' }, function(index) {
                $.getJSON("/WxSet/WxMenuSaveToWx", function(json) {
                    if (json.result == "success") {
                        layer.msg("保存成功", { icon: 1, time: 2000 });
                    } else {
                        layer.msg(json.msg, { icon: 5, time: 2000 });
                    }
                });
            });
        }
    </script>
}
